package controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;

public class CheckAdsenseSandboxServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String name = (String) request.getParameter("domain");
		
		String url = "http://pagead2.googlesyndication.com/pagead/ads?adtest=on&client=ca-digitalinspiration&random=1336472245&url=" + name + "&gl=default&color_border=336699&color_bg=FFFFFF&ad_type=image&format=250x250_as";
		String statusCode = null;
		try {
			statusCode = Integer.toString(Jsoup.connect(url).execute().statusCode());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("name", name);
		request.setAttribute("statusCode", statusCode);
		try {
			request.getRequestDispatcher("/pages/check_adsense_sandbox.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
